﻿using mortal_ClipboardSharing_windows.model;
using mortal_ClipboardSharing_windows.utils;
using System;
using System.Windows;

namespace mortal_ClipboardSharing_windows.user.register
{

    /// <summary>
    /// Register.xaml 的交互逻辑
    /// </summary>
    public partial class Register : Window
    {
        public delegate void PassValuesHandler(object sender, PassValuesEventArgs e);

        public event PassValuesHandler PassValuesEvent;

        public Register()
        {
            InitializeComponent();
        }

        // 注册
        private void Button_Click(object sender, RoutedEventArgs e)
        {

        }

        // 登录
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

            // 
            Api.Login(tb1.Text, tb2.Text, delegate (HttpResult<UserEntity> httpResult)
            {
                // 其实用全局变量已经能够实现两个窗口间的传值了，但是我又用代理做了一遍
                // 当初全局变量绝对不是为了传值用的，但是我不记得全局变量是为了干啥了。。。

                Global.TOKEN = httpResult.Data.Token;
                Global.NICKNAME = httpResult.Data.Nickname;

                // TODO 暂时先不做记住登录
                //Cache.SaveToken(Global.TOKEN);


                PassValuesEventArgs args = new PassValuesEventArgs(Global.TOKEN, string.Empty, Global.NICKNAME);
                PassValuesEvent(this, args);

                //this.BeginInvoke(new Action(this.Close));  // 使用委托关闭该窗体

                this.Dispatcher.BeginInvoke(new Action(() =>
                {
                    this.Close();
                }));
            });
        }
    }
}
